// pages/member/book/index.js
var t = getApp(),
  e = t.requirejs("core");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    route: "shop_coupon",
    menu: t.tabBar,
    menu_show: false,
    show: false,
    loaded: false,
    display:false,
    list: [],
    page: 1
  },
  //底部导航跳转
  menu_url: function (k) {
    e.menu_url(k, 2);
  },

  onGotUserInfo: function (q) {
    var that = this,
        cache = t.getCache("userinfo");

    // if (cache) {
    //     return;
    // }
    t.getUserInfo(q.detail.userInfo, function (t) {
      if (t != 1000) {
        var ucache = getApp().getCache("userinfo");
        if (ucache) {
          //var options = that.data.options;
          //that.getDetail(options)
          //getCurrentPages()[getCurrentPages().length - 1].onLoad();
          that.setData({
            display: false,
            //getPhoneAuth: getApp().globalData.getphone
          })
        }
      } else {
        that.setData({
          display: true
        })
      }

    }, q.detail.encryptedData, q.detail.iv);
  },

  // 授权获取用户信息判断
  authCheck: function () {
    var cache = getApp().getCache("userinfo");
    if (!cache.nickName && !cache.avatarUrl) {
      this.setData({
        display: true
      })
      return false;
    } else {
      this.setData({
        display: false
      })
      return true;
    }
  },

  // 取消登录
  cancel_auth: function () {
    this.setData({
      display: false
    })
  },

  onLoad: function (options) {

    if (options != null && options != undefined) {
      this.setData({
        tabbar_index: options.tabbar_index ? options.tabbar_index : -1
      });
    }

    e.setting();
    
   
    this.getList()
  },
  getList: function () {
    var t = this;
    e.get("Market/BusCoupon", {
      page: t.data.page,
      user_id: getApp().getCache("userinfo").uid
    }, function (i) {
      // console.log(i)
      if (i.code == 0) {
        var a = {
          show: true
        };
        i.info.length > 0 && (a.page = t.data.page + 1, a.list = t.data.list.concat(i.info), i.info.length < 10 && (a.loaded = true)),//concat() 方法用于连接两个或多个数组。
          i.info.length == 0 && (a.loaded = true);
        t.setData(a)
      } else {
        e.alert(i.msg)
      }
    }, true)
  },
  /**
  *上拉加载
  */
  onReachBottom: function () {
    this.data.loaded || this.getList()
  },

  /**
   *下拉刷新
   */
  onPullDownRefresh: function () {
    this.setData({
      page: 1,
      list: [],
      loaded: false
    }),
    this.getList()
    wx.stopPullDownRefresh()
  },
  /**
     *领券
     * @return string
     */
  getCoupon: function (t) {
    var auth_res = this.authCheck();
    if (!auth_res) {
      return;
    }
    
    var that = this,
      s = e.pdata(t);
      // console.log("张孟辉",s)
      // console.log('zhang',that.data)
    e.get("Market/GetCoupon", {
      coupon_id: s.id,
      end_time:s.endtime,
      get_count: s.get_count,
      user_id: getApp().getCache("userinfo").uid,
    }, function (i) {
      0 == i.code ? (
        e.alert('领券成功', function () {
          that.setData({
            page: 1,
            list: []
          }),
            that.getList()
        })
      ) : e.alert(i.msg)
    })
  },
  /**
   * 用户点击右上角分享
   */
  // onShareAppMessage: function () {
  //
  // }
})